<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">

      <el-form-item style="float: right;" label="" v-if="isAuth('hotel:hotelseller:list')">
        <selectSeller @change="selectSellerChange"></selectSeller>
      </el-form-item>
      <!--
      <el-form-item label="" v-if="isAuth('hotel:hotelseller:list')">
       <el-select
          multiple
           collapse-tags
          clearable
          style="width:180px;"
          v-model="sellerIds"
          filterable
          remote
          reserve-keyword
          @change="selectSeller"
          placeholder="商家名称"
          :remote-method="remoteMethod"
          :loading="loading"
        >
          <el-option v-for="item in platformSeller" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="房型名字" clearable></el-input>
      </el-form-item>
<!--      <el-form-item>
         <el-select v-model="dataForm.classify" placeholder="类型">
           <el-option label="默认" :value="''"> </el-option>
           <el-option label="钟点房" :value="0"> </el-option>
           <el-option label="全天房" :value="1"> </el-option>
         </el-select>
      </el-form-item> -->
      <el-form-item>
         <el-select v-model="dataForm.state" placeholder="APP">
           <el-option label="默认" :value="''"> </el-option>
           <el-option label="未上架" :value="0"> </el-option>
           <el-option label="已上架" :value="1"> </el-option>
         </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t('Common.Query') }}</el-button>
        <!-- <el-button @click="yijiangengxin()" v-if="isAuth('hotel:hotelroommoney:updateall')">一键更新</el-button> -->
        <el-button
          v-if="isAuth('hotel:hotelroom:save')"
          type="primary"
          @click="infoAddOrUpdate()"
        >{{ $t('Common.Insert') }}</el-button>
        <el-button
          v-if="isAuth('hotel:hotelroom:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
        >{{ $t('Common.BulkDelete') }}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border=""
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="sellerName" header-align="center" align="center" label="商家"></el-table-column>
      <el-table-column prop="logo" header-align="center" align="center" label="房型大图">
        <template slot-scope="scope">
          <img-view :imgSrc="scope.row.logo"></img-view>
        </template>
      </el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="房型名字"></el-table-column>
      <el-table-column prop="oprice" header-align="center" align="center" label="门市价"></el-table-column>
      <el-table-column prop="people" header-align="center" align="center" label="可住人数"></el-table-column>
      <el-table-column prop="totalNum" header-align="center" align="center" label="房间数量"></el-table-column>
      <!-- <el-table-column prop="yjCost" header-align="center" align="center" label="押金金额"></el-table-column> -->
      <el-table-column prop="sysState" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sysState === 0" size="small" type="danger" @click="state(scope.row.id,1)">禁用</el-tag>
          <el-tag v-else size="small" @click="state(scope.row.id,0)">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="state" header-align="center" align="center" label="APP">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state === 0" size="small" type="danger" @click="show(scope.row.id)">未上架</el-tag>
          <el-tag v-else size="small" @click="hide(scope.row.id)">已上架</el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column prop="classify" header-align="center" align="center" label="房间类别">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.classify === 0" size="small">钟点房</el-tag>
          <el-tag v-else size="small">全天房</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column fixed="right" header-align="center" align="center" width="200" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="mgrRoomMoenyHandler(scope.row)">价格管理</el-button>
          <el-button type="text" size="small" @click="infoAddOrUpdate(scope.row.id)">基本信息</el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">详细信息</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <InfoAddOrUpdate v-if="infoAddOrUpdateVisible" ref="infoAddOrUpdate" @refreshDataList="getDataList"></InfoAddOrUpdate>
    <RoomMoney v-if="mgrRoomMoenyVisible" ref="roomMoney"></RoomMoney>
  </div>
</template>

<script>
import AddOrUpdate from "./deta-index";
import InfoAddOrUpdate from "./info-add-or-update";
import RoomMoney from "./hotelroom-money";
export default {
  data() {
    return {
      loading:false,
      dataForm: {
        name: "",
        classify:1,
        state:''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      mgrRoomMoenyVisible: false,
      infoAddOrUpdateVisible: false,
      sellerIds:[],
      platformSeller:[]
    };
  },
  components: {
    AddOrUpdate,
    InfoAddOrUpdate,
    RoomMoney
  },
  activated() {
    this.getDataList();
    this.remoteMethod();
  },
  methods: {
    //一键更新会员最低价
    yijiangengxin() {
      this.$http({
        url: this.$http.adornUrl("/hotel/hotelroommoney/updateall"),
        method: "get",
        params: this.$http.adornParams()
      }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
      });
    },
    //加载平台商家数据
    remoteMethod(query) {
      if (this.isAuth('hotel:hotelseller:list')) {
        this.loading = true;
        this.$http({
          url: this.$http.adornUrl("/hotel/hotelseller/platformSeller"),
          method: "get",
          params: this.$http.adornParams({
            sellerName: query
          })
        }).then(({ data }) => {
          this.loading = false;
          if (data && data.code === 0) {
            this.platformSeller = data.page.list;
          } else {
            this.$message.error(data.msg);
          }
        });
      }
    },
    selectSellerChange(val) {
      console.log("接收值",val);
      this.sellerIds = val;
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/hotel/hotelroom/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          ...this.dataForm,
          ids:this.sellerIds.join(",")
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          console.log("data.page.list",data.page.list)
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    selectSeller(id, value) {
      this.getDataList()
    },
    state(id,state) {
      this.$http({
        url: this.$http.adornUrl(`/hotel/hotelroom/state/${id}`),
        method: "post",
        params: this.$http.adornParams({
          state:state
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.getDataList();
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    show(id) {
      this.$http({
        url: this.$http.adornUrl(`/hotel/hotelroom/show/${id}`),
        method: "post"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.getDataList();
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    hide(id) {
      this.$http({
        url: this.$http.adornUrl(`/hotel/hotelroom/hide/${id}`),
        method: "post"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.getDataList();
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 新增 / 修改
    infoAddOrUpdate(id) {
      this.infoAddOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.infoAddOrUpdate.init(id);
      });
    },
    //价格管理
    mgrRoomMoenyHandler(room) {
      this.mgrRoomMoenyVisible = true;
      this.$nextTick(() => {
        this.$refs.roomMoney.init(room);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/hotel/hotelroom/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    }
  }
};
</script>
